//
//  SEGExpressCollectionListViewController.m
//  ExcellentHome
//
//  Created by liangxinqiang on 15/9/25.
//  Copyright (c) 2015年 陈区. All rights reserved.
//

#import "SEGExpressCollectionListViewController.h"
#import "SEGExpressCollectionDetailViewController.h"

#import "SEGExpressCollectionListViewModel.h"

@interface SEGExpressCollectionListViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
/// ViewModel
@property (nonatomic, strong) SEGExpressCollectionListViewModel *viewModel;

@end

@implementation SEGExpressCollectionListViewController
 
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.title.length > 0? self.title : @"快递";
    [self showCustomNavigationGreedBackButton:SEGPicName(@"back") withPreImage:SEGPicName(@"back")];
    [self.view addSubview:self.tableView];
    // 缺省图
    [[SEGUhomeMediator sharedInstance] seg_showCustomProgressImage:@{@"view":self.view}];
    
    WeakSelf;
    [self.tableView addWindmillWithHeaderRefreshBlock:^{
        [weakSelf requestReceiveExpressIdAndList];
    }];
    [self.tableView beginHeaderRefreshing];
}

// 请求数据
- (void)requestReceiveExpressIdAndList {
    @weakify(self);
    [self.viewModel.signal subscribeNext:^(NSArray *dataArr) {
        @strongify(self);
        [self.tableView stopAnimatingWindmill];
        if (dataArr.count > 0) {
            [[SEGUhomeMediator sharedInstance] seg_hideCustomProgressImage:@{@"view":self.view}];
        } else {
            [[SEGUhomeMediator sharedInstance] seg_showCustomProgressImage:@{@"view":self.view}];
        }
        [self.tableView reloadData];
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.viewModel.objectArray count];
}
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 65;
}
#pragma mark - UITableViewDelegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdStr = @"CellReuse";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdStr];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdStr];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        UIImageView* line = [[UIImageView alloc] initWithFrame:CGRectMake(10, 64.5, CGRectGetWidth(self.view.bounds), 0.5)];
        line.image = [UIImage imageWithContentsOfFile:SEGPicName(@"common_line610px.png")];
        line.tag = 1000;
        [cell.contentView addSubview:line];

        UILabel* companyName = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 200, 14)];
        companyName.font = [UIFont systemFontOfSize:FONT_LIST_AND_DETAIL_SIZE];
        companyName.backgroundColor = [UIColor clearColor];
        companyName.textColor = kColor_1;
        companyName.tag = 1001;
        [cell.contentView addSubview:companyName];
        
        UILabel* receiverName = [[UILabel alloc] initWithFrame:CGRectMake(20, 32, 200, 12)];
        receiverName.font = [UIFont systemFontOfSize:FONT_DESCRIPTION_SIZE];
        receiverName.backgroundColor = [UIColor clearColor];
        receiverName.textColor = kColor_1;
        receiverName.tag = 1002;
        [cell.contentView addSubview:receiverName];
        
        UILabel* receiveTime = [[UILabel alloc] initWithFrame:CGRectMake(20, 50, 200, 10)];
        receiveTime.font = [UIFont systemFontOfSize:FONT_ANNOTATION_SIZE];
        receiveTime.backgroundColor = [UIColor clearColor];
        receiveTime.textColor = kColor_2;
        receiveTime.tag = 1003;
        [cell.contentView addSubview:receiveTime];
        
        UILabel* state = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.view.bounds) - 50, 0, 40, 65)];
        state.font = [UIFont systemFontOfSize:FONT_LIST_AND_DETAIL_SIZE];
        state.backgroundColor = [UIColor clearColor];
        state.textColor = kColor_T;
        state.tag = 1004;
        [cell.contentView addSubview:state];
    }
    
    SEGExpressModel *model = [self.viewModel.objectArray objectAtIndex:indexPath.row];
    
    UILabel *companyName = (UILabel*)[cell.contentView viewWithTag:1001];
    UILabel *receiverName = (UILabel*)[cell.contentView viewWithTag:1002];
    UILabel *receiveTime = (UILabel*)[cell.contentView viewWithTag:1003];
    UILabel *state = (UILabel*)[cell.contentView viewWithTag:1004];
    
    companyName.text = model.expressCompanyName;
    receiverName.text = model.expressReceiveName;
    receiveTime.text = model.expressReceiveTime;
    state.text = model.expressStatus;
    
    return cell;
}
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    SEGExpressModel *aExpress = [self.viewModel.objectArray objectAtIndex:indexPath.row];
    SEGExpressCollectionDetailViewController *detailView = [[SEGExpressCollectionDetailViewController alloc] init];
    detailView.expressID = aExpress.expressSid;
    detailView.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detailView animated:YES];
}

#pragma mark - Getter
- (SEGExpressCollectionListViewModel *)viewModel {
    if (!_viewModel) _viewModel = [[SEGExpressCollectionListViewModel alloc] init];
    return _viewModel;
}
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, LYScreenWidth(), LYScreenHeight() - kNavigationBarAndStatusBarHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundView = nil;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    }
    return _tableView;
}

@end
